package com.example.test003.controller;

import com.example.test003.domain.ResponseResult;
import com.example.test003.enums.AppHttpCodeEnum;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Base64;

@RestController
@RequestMapping("/pic")
public class PicController {

    @GetMapping("/FyPic")
    public ResponseResult getFyPic(String type,String id) throws Exception {
        String path = null;
        if ("tp".equals(type)){
            path = "D:\\Project\\QQFY\\20230102\\tp\\tpFigure\\"+id+".png";
            File imageFile = new File(path);
            try {
                byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
                String base64Image = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(imageBytes);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.TEXT_PLAIN);
                return ResponseResult.okResult(base64Image);
            } catch (IOException e) {
                // 处理异常
                e.printStackTrace();
                return ResponseResult.errorResult(AppHttpCodeEnum.PIC_ID_ERROR);
            }
        } else if ("t2m".equals(type)) {
            path = "D:\\Project\\QQFY\\20230102\\t2m\\t2mFigure\\"+id+".png";
            File imageFile = new File(path);
            try {
                byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
                String base64Image = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(imageBytes);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.TEXT_PLAIN);
                return ResponseResult.okResult(base64Image);
            } catch (IOException e) {
                // 处理异常
                e.printStackTrace();
                return ResponseResult.errorResult(AppHttpCodeEnum.PIC_ID_ERROR);
            }
        }else if ("sp".equals(type)) {
            path = "D:\\Project\\QQFY\\20230102\\sp\\spFigure\\"+id+".png";
            File imageFile = new File(path);
            try {
                byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
                String base64Image = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(imageBytes);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.TEXT_PLAIN);
                return ResponseResult.okResult(base64Image);
            } catch (IOException e) {
                // 处理异常
                e.printStackTrace();
                return ResponseResult.errorResult(AppHttpCodeEnum.PIC_ID_ERROR);
            }
        }else if ("r2".equals(type)) {
            path = "D:\\Project\\QQFY\\20230102\\r2\\r2Figure\\"+id+".png";
            File imageFile = new File(path);
            try {
                byte[] imageBytes = Files.readAllBytes(imageFile.toPath());
                String base64Image = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(imageBytes);
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.TEXT_PLAIN);
                return ResponseResult.okResult(base64Image);
            } catch (IOException e) {
                // 处理异常
                e.printStackTrace();
                return ResponseResult.errorResult(AppHttpCodeEnum.PIC_ID_ERROR);
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PIC_TYPE_ERROR);
        }

    }

    @GetMapping("/FyPicPath")
    public ResponseResult getFyPicPath(String type,String id) throws Exception {
        if ("tp".equals(type)){
            String path = "D:\\Project\\QQFY\\20230102\\tp\\tpFigure\\"+id+".png";
            return ResponseResult.okResult(path);
        } else if ("t2m".equals(type)) {
            String path = "D:\\Project\\QQFY\\20230102\\t2m\\t2mFigure\\"+id+".png";
            return ResponseResult.okResult(path);
        } else if ("sp".equals(type)) {
            String path = "D:\\Project\\QQFY\\20230102\\sp\\spFigure\\"+id+".png";
            return ResponseResult.okResult(path);
        } else if ("r2".equals(type)) {
            String path = "D:\\Project\\QQFY\\20230102\\r2\\r2Figure\\"+id+".png";
            return ResponseResult.okResult(path);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PIC_TYPE_ERROR);
        }
    }
}
